package zozopo.user;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class CheckUsernameAction extends ActionSupport implements Action,SessionAware,ServletRequestAware,ServletResponseAware{
	//传入参数
	private String username;
	
	//传出参数
	private String responseText  = "";
	
	HttpServletRequest request;
	HttpServletResponse response;
	private Map<Object, Object> session;
	
	
	public String execute(){
		
		UserOS userOS = new UserOS();
		
	
		UserTB user = userOS.getUserInfoByName(username);
		if(user == null){
			responseText = "\""+username+"\" 用户名称未被注册";
		}else{
			responseText = "\""+username+"\" <font color='red'>用户已经存在</font>";
		}
			
		userOS.releaseConnection();
		
		
		return SUCCESS;
	} 
	
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml; charset=UTF-8");
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) throws UnsupportedEncodingException {
		this.username = java.net.URLDecoder.decode(username, "UTF-8");
	}


	public String getResponseText() {
		return responseText;
	}


	public void setResponseText(String responseText) {
		this.responseText = responseText;
	}
}
